<?php
namespace yan\hkecship\box;
class Box{
	/**
	 * mm
	 * g
	 * */
	public $width;
	public $height = 900; //假設
	public $depth = 1500;
	public $weight = 30000; // <10 – 30公斤
	public $free_wieght = 0;
	public $items = []; //已添加的箱子
	public $free = []; //剩餘空間
	public $free_key = 0;
	public $volume = 0;
	public function __construct(){
		$this->width = $this->getWidth();
		$this->free[] = ['x'=>$this->width, 'y'=>$this->height, 'z'=>$this->depth, 'b'=>0, 'is_use'=>0];
		$this->free_wieght = $this->weight;
	}
	/**
	 * 周長 + 長度 : 3米(XP)
	 * 長度 : 1.5米 (XP)
	 * */
	public function getWidth(){
		return 1500 - $this->height;
	}
	public function addItem(Item $item){
		if($this->canAdd($item)){
			$this->items[] = $item;
			return true;
		}
		return false;
	}
	public function canAdd(Item $item){
		$max = $item->getMax();
		if($item->is_add == 0 && $max <= $this->depth && $item->weight <= $this->free_wieght){
			foreach ($this->free as $key => $free) {
				if($free['is_use']==0 && $free['x'] >= $item->width && $free['y']>=$item->height && $free['z'] >= $item->depth){
					$this->free_key = $key;
					return true;
				}
			}
		}
		return false;
	}
	public function push(Item $item){
		$free = $this->free[$this->free_key];
		if($item->depth <= $free['x']){
			$x = $free['x'] - $item->depth;
			$y = $free['y'] - $item->height;
			$z = $free['z'] - $item->width;
			$item->position = 'z.y.x';
			
			$min = min($x, $y, $z);
			if($min < 0 ){
				$x = $free['x'] - $item->depth;
				$y = $free['y'] - $item->width;
				$z = $free['z'] - $item->height;
				$item->position = 'z.x.y';
			}
		}else{			
			if($item->depth <= $free['y']){
				$x = $free['x'] - $item->height;
				$y = $free['y'] - $item->depth;
				$z = $free['z'] - $item->width;
				$item->position = 'y.z.x';
				$min = min($x, $y, $z);
				if($min < 0 ){
					$x = $free['x'] - $item->width;
					$y = $free['y'] - $item->depth;
					$z = $free['z'] - $item->height;
					$item->position = 'x.z.y';
					$min = min($x, $y, $z);
					if($min < 0){
						$x = $free['x'] - $item->width;
						$y = $free['y'] - $item->height;
						$z = $free['z'] - $item->depth;
						$item->position = 'x.y.z';
					}
				}
			}else{
				$x = $free['x'] - $item->width;
				$y = $free['y'] - $item->height;
				$z = $free['z'] - $item->depth;
				$item->position = 'x.y.z';
			}
		}
		$free_fenge = [
			[$free['x'], $y, $free['z']],
			[$free['x'], $free['y'], $z],
			[$x, $item->height, $item->depth],
		];
		$this->free[$this->free_key]['is_use'] = 1;
		foreach ($free_fenge as $key => $val) {
			$this->free[] = [
				'x' => $val[0],
				'y' => $val[1],
				'z' => $val[2],
				'is_use' => 0,
				'b' => $this->free_key,
			];
		}
		$this->free_wieght -= $item->weight;
		$this->volume += $item->volume;
		$item->is_add = 1;
		$this->items[] = $item;
	}
	public function getTotalWeight(){
		return $this->weight - $this->free_wieght;
	}
	public function getBoxInfo(){
		if(count($this->items)<2){
			$item = $this->items[0];
			return [$item->width, $item->height, $item->depth, $item->weight];
		}
		$cubeRoot = pow($this->volume, 1/3);
		$cubeRoot = ceil($cubeRoot);
		if($cubeRoot > $this->depth){
			return [$this->width, $this->height, $this->depth, $this->getTotalWeight()];
		}
		if($cubeRoot > $this->width){
			$depth = $this->volume / ($this->width * $this->height);
			$depth = $depth > $this->depth ? $this->depth : $depth;
			return [$this->width, $this->height, $depth, $this->getTotalWeight()];
		}
		return [$cubeRoot, $cubeRoot, $cubeRoot, $this->getTotalWeight()];
	}
}